<!-- home -->
<template>
  <div class="turnTable-container" :style="backgroundStyle" v-if="isShow">
    <div class="frame">
      <!-- <div class="frameRule" @click="isRule">活动规则</div> -->
      <img class="frameRule" @click="isRule" src="../../../static/rule.png" alt="" />
      <img class="frameRule frameCard" @click="myCard" src="../../../static/jilu.png" alt="" />
      <img class="frameRule2 frameCard" @click="updatePhoneShow = true" src="../../../static/bind.png" alt="" />
     <!-- <div class="frameRule frameCard" @click="myCard">中奖记录</div>
      <div class="frameRule2 frameCard" @click="updatePhoneShow = true">更换手机号</div> -->
    </div>
    <div>
      <!-- <img class="imgTitle" :src="activity.url"> -->

    <div class="lunpan">
      <img :src="activity.goodsImgs" id="lotteryBtn" alt="" class="turnImg"
        :style="'transform: rotate(' + deg + 'deg);  transition-duration: ' + time + 's ;'" />
      <img :src="activity.pointerImg" id="btn" alt="" class="arrow" />
    </div>
    <img :src="activity.baseImg" alt="" class="dizuo" />
    </div>
    <div class="spanStyleView">
      <!-- <span class="words">剩余次数:</span><span class="numbers">{{ number }}次</span><span class="words"></span> -->
    </div>
    <div class="luckDrawStyle" :style="backButton" @click="submitPlace()"></div>


    <activityRule :show="ruleShow" :title="mallActivityEntity.strategy" @isRule="isRule"></activityRule>
    <changePhone v-if="updatePhoneShow" @phoneCancel="updatePhoneShow = false" :titles="titles"></changePhone>
  </div>
</template>

<script>
  // 请求接口
  import {
    activityInfo2,
    activityVerify,
    draw,
    placeOrder,
    postPrepay,
    getUserInfo
  } from '@/api/raffle.js'
  import changePhone from '@/components/changePhone'
  import activityRule from '@/components/activityRule'
  import { setStore, getStore, removeStore } from '@/utils/storage'
import { baseApi, wechatAuthUrlTail, preLoginUrl } from '@/config'
import axios from 'axios'
  import {
    Dialog
  } from 'vant'

  export default {
    components: {
      changePhone,
      activityRule
    },
    data() {
      return {
        ruleShow: false,
        deg: 0,
        time: 0,
        number: 0, //
        show: false,
        activityCode: '', //活动信息唯一code
        updatePhoneShow: false, //手机号弹窗
        mallActivityEntity: {}, //活动信息
        activity: {}, //活动内容
        isSettimer: false,
        titles: '绑定手机号',
        goodsType: '',
        orderSn:'',
        isShow:false
      }
    },
  computed: {
    // 背景图
    backgroundStyle() {
      return {
        backgroundImage: `url(${this.activity.backgroundImg})`,
        backgroundSize: '100% 100%', // 根据需要设置
      };
    },
    backButton() {
      return {
        backgroundImage: `url(${this.activity.buttonImg})`,
        backgroundSize: '100% 100%', // 根据需要设置
      };
    },
  },
    created(){
    },
    mounted() {
      this.activityCode = this.$route.query.activityCode
      setStore('activityCode',this.$route.query.activityCode)
      this.activityInfo2()
    },
    methods: {
      submitPlace(){
        let that = this
        let data ={
          activityId:this.mallActivityEntity.id
        }
        placeOrder(data).then(res=>{
          console.log(res)
            that.orderSn = res.data.orderSn
            if(res.code==0){
              if(res.data.needToPay==1){
                let pay = {
                  orderId:res.data.orderSn,
                  tradeType:'JSAPI'
                }
                that.prepay(pay)
              }else{
                that.start()
              }
            }else{
              that.showToast(res.msg)
            }
        }).catch(res=>{
          console.log(1)
          if(res.code==7){
            this.updatePhoneShow = true
          }else{
            that.showToast(res.msg)
          }
        })
      },
      //预支付(拿到微信支付参数)
      prepay(pay){
        let that = this
        postPrepay(pay).then(res=>{
          console.log(res)
          if (res.msg === 'success') {
            if (typeof WeixinJSBridge === 'undefined') {
              if (document.addEventListener) {
                document.addEventListener('WeixinJSBridgeReady', that.onBridgeReady, false)
              } else if (document.attachEvent) {
                document.attachEvent('WeixinJSBridgeReady', that.onBridgeReady)
                document.attachEvent('onWeixinJSBridgeReady', that.onBridgeReady)
              }
            } else {
              that.onBridgeReady(res.data)
            }
          }
        }).catch(fail=>{
          that.showToast(fail.msg)
        })
      },
      // 微信支付预支付OK时，发起真正微信支付请求
      onBridgeReady(data) {
        const that = this
        WeixinJSBridge.invoke('getBrandWCPayRequest', {
            appId: data.appId, // 公众号名称，由商户传入
            timeStamp: data.timeStamp, // 时间戳，自1970年以来的秒数
            nonceStr: data.nonceStr, // 随机串
            package: data.packageValue, // 微信订单id
            signType: 'MD5', // 微信签名方式：
            paySign: data.paySign // 微信签名
          },
          function(res) {
            if (res.err_msg === 'get_brand_wcpay_request:ok') {
              that.showToast('支付成功...')
              setTimeout(() => {
               that.start()
              }, 1000);
            } else {
              that.showToast('未完成支付...')
              return
            }
          })
      },
      // 抽奖
      start() {
        console.log(31)
        if (this.isSettimer) return
        let data = {
          activityId: this.mallActivityEntity.id,
          number: 1,
          orderSn:this.orderSn
        }
        this.deg = 0
        draw(data).then(res => {
          if (res.code == 0) {
            this.isSettimer = true
            this.deg = -(1080 + res.data.angle)
            this.time = 5
            this.number = res.data.number
            this.goodsType = res.data.goodsType
            setTimeout(() => {
              this.isSettimer = false
              this.time = 0
              if (!res.data.lotteryBool) {
                this.dialog('非常遗憾未中奖')
              } else {
                this.dialog('恭喜您抽中了' + res.data.goodsName + '请点击中奖记录查看')
              }
            }, 6000)
          } else {
            this.dialog(res.msg)
          }
        }).catch(res => {
          if (res.code == 7) {
            this.updatePhoneShow = true
          }
          this.dialog(res.msg)
        })
      },
      // 活动规则
      isRule() {
        this.ruleShow = !this.ruleShow
      },
      // 活动卡卷1
      myCard() {
        this.$router.push({
          path: '/mycard?id='+this.mallActivityEntity.id
        })
      },
      //活动信息
      activityInfo2() {
        let data = {
          activityCode: this.activityCode
        }
        axios({
          method: 'get',
          url: baseApi + 'app/wheelLottery/activityInfo',
          params: {
             activityCode: this.activityCode
          }
        })
        .then(res => {
          let data = res.data.data
          console.log(data)
          setStore('appid',data.mallActivityEntity.appId)


          if(!getStore('token')){
            let wechatAuthUrlFront = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid='+data.mallActivityEntity.appId+'&redirect_uri='
            var jumpUrl = wechatAuthUrlFront + encodeURIComponent(preLoginUrl) + wechatAuthUrlTail
            console.log(jumpUrl)
            setStore('loginRequestUrl',window.location.href)
            setTimeout(function() {
              location.href = jumpUrl
            }, 1500)
          }else{
            this.mallActivityEntity = data.mallActivityEntity
            this.activity = data
            document.title = data.mallActivityEntity.name
            this.isShow = true
          }
        })
        .catch(res => {
        })


        // activityInfo2(data).then(res => {
        //   setStore('appid',res.data.mallActivityEntity.appId)
        //   this.mallActivityEntity = res.data.mallActivityEntity
        //   this.activity = res.data
        //   document.title = res.data.mallActivityEntity.name

        //   if(!getStore('token')){
        //     let wechatAuthUrlFront = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid='+getStore('appid')+'&redirect_uri='
        //     var jumpUrl = wechatAuthUrlFront + encodeURIComponent(preLoginUrl) + wechatAuthUrlTail

        //     setTimeout(function() {
        //       location.href = jumpUrl
        //     }, 1500)
        //   }
        // })
      },

      // 获取白名单资格
      activityVerify() {
        let data = {
          id: this.mallActivityEntity.id
        }
        activityVerify(data).then(res => {
          if (res.code == 0) {
            this.number = res.data.number
          } else {
            this.dialog(res.msg)
          }
        }).catch(res => {
          if (res.code == 7) {
            this.updatePhoneShow = true
          }
          this.dialog(res.msg)
        })
      },
      // 提示
      dialog(message, url) {
        Dialog.alert({
          message: message,
        }).then(() => {
          // on close
          if (url) {

          }
        });
      },
      showToast(msg) {
        this.$toast({
          message: msg,
          position: 'center',
          duration: 2500
        })
      },
    }
  }
</script>
<style lang="less">
  .frameCard {
    margin-top: 10px;
  }

  .frameRule {
    // width: 26px;
    color: #fff;
    border-radius: 12px 0 0 12px;
    box-sizing: border-box;
    display: block;
  }
  .frameRule2{
    display: block;
    // width: 26px;
    color: #fff;
    border-radius: 12px 0 0 12px;
    box-sizing: border-box;
  }

  .frame {
    position: absolute;
    top: 20px;
    right: 0;
  }
  .words {
    color: rgb(64, 171, 156);
    font-size: 16px;
  }

  .numbers {
    font-size: 16px;
    color: rgb(64, 171, 156);
  }

  .himalayan-fanhui {
    // width: 55px;
    height: 26px;
    background: #fde2c0;
    opacity: 1;
    border-radius: 5px;
    position: absolute;
    top: 7px;
    left: 5px;
    display: flex;
    align-items: center;
    font-size: 17px;
    color: #cc2710;
    font-weight: bold;
    box-sizing: border-box;
    opacity: 0.7;
    padding: 4px 8px;
  }

  .robotStyle {
    position: absolute;
    right: 25px;
    bottom: 100px;
    width: 125px;
    z-index: 9999;
  }

  .uuidImg {
    width: 110px;
  }

  .turnTable-container {
    /* 你的命名空间 */
    background: #fff;
    background: url('https://zzbank-1302786294.cos.ap-guangzhou.myqcloud.com/shops/images/20230330/74440910471146309904a237c62bc7b1.png')
      no-repeat;
    background-size: 100% 100%;
    min-height: 100vh;
    box-sizing: border-box;
    // overflow-x: hidden;
    position: relative;
    overflow: hidden;
    position: relative;
  }
  .xuhuan{
    width: 100%;
    height: 100%;
    position: absolute;
    display: block;
    overflow: hidden;
  }

  .model {
      width: 100%;
      height: 100%;
      position: fixed;
      top: 0;
      left: 0;
      background: rgba(0, 0, 0, 0.5);
      z-index: 100;


      // .acb {
      //   background: url('../../../static/new_icon/pop.png') no-repeat;
      //   background-size: 100% auto;
      // }

      .onethreeModel {
        width: 332px;
        height: 384px;
        position: absolute;
        top: 40%;
        left: 52%;
        transform: translate(-50%, -50%);

        .onethreeModel_view {
          width: 258px;
          // height: 170px;
          margin: 0 auto;
          margin-top: 130px;
          box-sizing: border-box;

          h3 {
            font-size: 15px;
            text-align: center;
            color: #333;
            padding-top: 10px;
            font-weight: 400;
          }

          p {
            text-align: center;
            color: #58301a;
            font-size: 15px;
            letter-spacing: 2px;
            font-weight: bold;
          }

          .onethreeModel_input {
            box-sizing: border-box;
            padding-top: 30px;
            width: 258px;
            height: 220px;
            background: linear-gradient(180deg, #f9ebe4 0%, #f8bda1 100%);
            margin: 0 auto;
            box-sizing: border-box;
            margin-top: 10px;
            border-radius: 8px;
            padding: 5px;

            .onethreeModel_inpu_bor {
              width: 100%;
              height: 160px;
              border-radius: 8px;
              // border: 1px solid rgba(199, 68, 33, 0.47843137254901963);
              padding: 20px 8px;
              box-sizing: border-box;
              margin-top: 10px;
            }

            .onethreeModel_item_input {
              display: flex;
              align-items: center;
              justify-content: space-between;
              background: #fff;
              box-sizing: border-box;
              margin-bottom: 10px;
              border-radius: 8px;
              width: 100%;

              .onemel_left {
                width: 40px;
                height: 33px;
                display: flex;
                align-items: center;
                justify-content: center;
                flex-grow: 0;
                flex-shrink: 0;
              }

              input {
                border: 0;
                outline: 0;
                padding-left: 10px;
                flex: 1;
                font-size: 11px;
              }

              .yzm {
                color: #d82715;
                font-size: 10px;
                flex-grow: 0;
                flex-shrink: 0;
                flex: 1;
                text-align: right;
                padding-right: 5px;
              }
            }
          }

          .stasuccess_btn {
            width: 127px;
            height: 30px;
            background: linear-gradient(180deg, #fdd0a6 0%, #eba965 100%);
            box-shadow: 0px 3px 5px rgba(197, 34, 34, 0.64);
            opacity: 1;
            border-radius: 23px;
            font-size: 18px;
            line-height: 30px;
            display: block;
            margin: 0 auto;
            text-align: center;
            color: #d40606;
          }

          .onethreeModel_btn {
            width: 127px;
            height: 31px;
            background: #da371f;
            box-shadow: 0px 2px 5px rgba(236, 74, 74, 0.61);
            opacity: 1;
            border-radius: 23px;
            display: block;
            margin: 0 auto;
            font-size: 19px;
            color: #fff;
            letter-spacing: 3px;
            text-align: center;
            line-height: 31px;
            margin-top: 23px;
          }
        }
      }

      .model_cha {
        width: 35px;
        height: 35px;
        position: absolute;
        bottom: -50px;
        left: 50%;
        transform: translate(-50%, 0%);
      }
    }

    @media (min-height:375px) {
      .title_logo{
        width: 160px;
        height: 25px;
        margin: 25px 0 0 20px;
      }
      .title_name{
        width: 250px;
        height: 47px;
        margin: 10px 0 0 62.5px;
      }
      .tip{
        width: 100%;
        height: 16px;
        font-size: 12px;
        color: rgb(197, 117, 56);
        margin-top: 5px;
        text-align: center;
        font-weight: bold;
      }
      .lunpan{
        width: 275px;
        height: 275px;
        margin: 180px auto 0;
        position: relative;
        z-index: 3;
      }
      .turnImg {
        width: 100%;
        height: 100%;
      }
      .arrow {
        width: 35px;
        height: 44px;
        position: absolute;
        top: 50%;
        left: 50%;
        transform: translate(-50%,-50%);
        z-index: 3;
      }
      .dizuo{
        width: 313px;
        height: 121px;
        margin: 0 auto;
        display: block;
      }
      .spanStyleView {
        width: 100%;
        text-align: center;
        letter-spacing: 2px;
        position: fixed;
        bottom: 80px;
      }
      .luckDrawStyle {
        width: 240px;
        height: 50px;
        background: url('https://zzbank-1302786294.cos.ap-guangzhou.myqcloud.com/shops/images/20230330/e00362afed4e4e8eb5d8c22f5a30d5cb.png')
          no-repeat;
        background-size: 100% 100%;
        font-size: 29px;
        line-height: 50px;
        font-weight: bold;
        text-align: center;
        letter-spacing: 1px;
        color: #FC0E07;
        position: absolute;
        bottom: 15px;
        left: 50%;
        transform: translateX(-50%);
      }
    }
    @media (min-height:600px) {
      .title_logo{
        width: 160px;
        height: 25px;
        margin: 15px 0 0 20px;
      }
      .title_name{
        width: 250px;
        height: 47px;
        margin: 10px 0 0 62.5px;
      }
      .tip{
        width: 100%;
        height: 16px;
        font-size: 13px;
        color: rgb(197, 117, 56);
        margin-top: 5px;
        text-align: center;
        font-weight: bold;
      }
      .lunpan{
        width: 275px;
        height: 275px;
        margin: 190px 0 0 50px;
        position: relative;
        z-index: 3;
      }
      .turnImg {
        width: 100%;
        height: 100%;
      }
      .arrow {
        width: 35px;
        height: 44px;
        position: absolute;
        top: 50%;
        left: 50%;
        transform: translate(-50%,-50%);
        z-index: 3;
      }
      .dizuo{
        width: 313px;
        height: 121px;
        margin: 0 auto;
        display: block;
      }
      .spanStyleView {
        width: 100%;
        text-align: center;
        letter-spacing: 2px;
        position: fixed;
        bottom: 85px;
      }
      .luckDrawStyle {
        width: 240px;
        height: 50px;
        background: url('https://zzbank-1302786294.cos.ap-guangzhou.myqcloud.com/shops/images/20230330/e00362afed4e4e8eb5d8c22f5a30d5cb.png')
          no-repeat;
        background-size: 100% 100%;
        font-size: 29px;
        line-height: 50px;
        font-weight: bold;
        text-align: center;
        letter-spacing: 1px;
        color: #FC0E07;
        position: absolute;
        bottom: 15px;
        left: 50%;
        transform: translateX(-50%);
      }
    }
    @media (min-height:700px) {
      .title_logo{
        width: 185px;
        height: 29px;
        margin: 15px 0 0 20px;
      }
      .title_name{
        width: 320px;
        height: 60px;
        margin: 10px 0 0 28px;
      }
      .tip{
        width: 100%;
        height: 16px;
        font-size: 16px;
        color: rgb(197, 117, 56);
        margin-top: 10px;
        text-align: center;
        font-weight: bold;
      }
      .lunpan{
        width: 325px;
        height: 325px;
        margin: 240px auto 0;
        position: relative;
        z-index: 3;
      }
      .turnImg {
        width: 100%;
        height: 100%;
      }
      .arrow {
        width: 62px;
        height: 85px;
        // height: 87px;
        position: absolute;
        top: 50%;
        left: 50%;
        transform: translate(-50%,-50%);
        z-index: 3;
      }
      .dizuo{
        width: 313px;
        height: 121px;
        margin: 0 auto;
        display: block;
      }
      .spanStyleView {
        width: 100%;
        text-align: center;
        letter-spacing: 2px;
        position: fixed;
        bottom: 100px;
      }
      .luckDrawStyle {
        width: 240px;
        height: 50px;
        font-size: 29px;
        line-height: 50px;
        font-weight: bold;
        text-align: center;
        letter-spacing: 1px;
        color: #FC0E07;
        position: absolute;
        bottom: 30px;
        left: 50%;
        transform: translateX(-50%);
      }
    }
</style>
